var express = require('express');
var router = express.Router();

let {Msg , curPath} = require('../tools/msg.js');

let mysqlExec = require('../db/dbMySql.js');


 // 首页用到的所有接口

 //轮播图接口
router.get('/banners',async (req,res)=>{
  //  let sql = `select * from e_banners`;
   let sql = `select title ,concat('${curPath}banners/',image_src) as image_src from e_banners where isshow=1`;

   let[e, d] = await mysqlExec(sql);
  //  res.send(d);
  if(e){
    // res.send({status:200, msg:"首页轮播图查询成功",data:d});
    res.send(Msg(200,"首页轮播图查询成功",d));
  }else{
    // res.send({status:500, msg:"首页轮播图查询失败"});
    res.send(Msg(500,'首页轮播图查询失败'));
  }
});

//首页课程查询
router.get('/course', async(req,res)=>{
  // let sql =`select * from e_course `;
  let {cid= 1,limits = 8}= req.query;
  let start = 0;
  let sql =`select cid,title,price,type, concat('${curPath}course/',image_src) as image_src,type_name,area_name,subject_name from e_course where type=${cid} limit ${start} , ${limits}`;
  let [e,d]=await mysqlExec(sql);
  if(e){
    res.send(Msg(200,"首页课程数据查询成功",d));
  }else{
    res.send(Msg(500, "首页课程数据查询失败"));
  }
})

module.exports = router;
